﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Authentication.Core.Data.Repository;
using Common.Contract.Authentication.Identifier;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace Authentication.Provider
{
    public class AccountRoleProvider : GenericProvider, IUserRoleProvider
    {
        public bool Update(ApplicationAccountRule obj)
        {
            try
            {
                var appKey = obj.ApplicationKey.ToUpper();
                var userName = obj.UserName;
                var objectId = obj.ObjectId;
                var ruleId = obj.RuleId;
                var GroupChecked = repository.FindOne<ApplicationAccountRule>(x => x.UserName == userName &&
                                                                                 x.ApplicationKey.ToUpper() == appKey &&
                                                                                 x.ObjectId == objectId &&
                                                                                 x.RuleId == ruleId);
                if (GroupChecked == null)
                    repository.AccountRoles.Add(obj);
                else
                    GroupChecked.Active = obj.Active;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public ApplicationAccountRule Retrieve(Guid applicationKey, string username, string objectid, string roleidentifier)
        {
            int ruleId = -1;
            if (RoleIdentifier.IsValid(roleidentifier))
                ruleId = new RoleIdentifier(roleidentifier).PersistableID;

            var appKey = applicationKey.ToString().ToUpper();
            var userChecked = repository.GetQuery<ApplicationAccountRule>(x => x.UserName == username &&
                                                                                x.ApplicationKey.ToUpper() == appKey &&
                                                                                x.ObjectId == objectid &&
                                                                                x.RuleId == ruleId).Include(x => x.Object);
            return userChecked.FirstOrDefault();

        }

        public IEnumerable<ApplicationAccountRule> RetrieveUserRules(Guid applicationKey, string username = "")
        {
            var appkey = applicationKey.ToString().ToUpper();
            IEnumerable<ApplicationAccountRule> result = repository.GetQuery<ApplicationAccountRule>(x => x.ApplicationKey.ToUpper() == appkey).Include(x => x.Object);
            if (!string.IsNullOrEmpty(username))
                result = result.Where(x => x.UserName.ToUpper() == username.ToUpper());

            return result.ToList();
        }

        public bool Delete(ApplicationAccountRule grouprole)
        {
            var appKey = grouprole.ApplicationKey.ToUpper();
            var groupId = grouprole.UserName;
            var objectId = grouprole.ObjectId;
            var ruleId = grouprole.RuleId;
            var userChecked = repository.FindOne<ApplicationAccountRule>(x => x.UserName == groupId &&
                                                                                x.ApplicationKey.ToUpper() == appKey &&
                                                                                x.ObjectId == objectId &&
                                                                                x.RuleId == ruleId);
            if (userChecked == null)
                return false;

            repository.AccountRoles.Remove(userChecked);
            repository.UnitOfWork.SaveChanges();
            return true;
        }
    }
}
